﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TfsRollUp.Core;

namespace TfsRollUp.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class WorkItemNodeTest
    {
        int _totalUnitOfTime;
        int _numberOfNodes = 0;
        Random _random;

        /// <summary>
        /// ctor
        /// </summary>
        public WorkItemNodeTest()
        {
            _random = new Random();
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        /// <summary>
        /// Sum must be calculated correct
        /// </summary>
        [TestMethod]
        public void SumMustBeCalculatedCorrect()
        {
            var root = CreateRandomTree();
            var total = root.CalculateChildren();
            Assert.IsTrue(total > 0);
            Assert.AreEqual(total, _totalUnitOfTime);
        }

        private int GetHours()
        {
            int random = _random.Next(1, _random.Next(15, 30));
            _totalUnitOfTime += random;
            return random;
        }

        private WorkItemNode CreateRandomTree()
        {
            var root = new WorkItemNode(null, 0, 1);
            _numberOfNodes = 1;
            CreateChildren(root, _random.Next(2, 5));
            return root;
        }

        private void CreateChildren(WorkItemNode parent, int numberOfChildren)
        {
            for (int i = 0; i < numberOfChildren; i++)
            {
                var node = new WorkItemNode(parent, _numberOfNodes++, GetHours());
                for (int level = 0; level < _random.Next(0, 3); level++)
                {
                    CreateChildren(node, _random.Next(0, 3));
                }
            }
        }
    }
}
